package net.chasing.androidbaseconfig.util;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * 数字处理工具类
 */
public class NumberUtils {

    /**
     * GUID是一个128位长的数字，一般用16进制表示。算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成GUID,
     * 可以产生一个号称全球唯一的ID
     * 生成的字符串格式：5e562cc9-8a92-3592-9528-3e444116061a
     * 总长36
     */
    public static String GenerateGUID() {
        return UUID.randomUUID().toString().toLowerCase();
    }

    /**
     * 原GUID基础上，去掉 - ，生成32位字符串
     */
    public static String GenerateGUID32() {
        return GenerateGUID().replace("-", "");
    }

    // 把低位在前高位在后的字节数组变成整型
    public static int byte2IntBack(byte[] bytes, int offset, int count) {
        int sum = 0;
        for (int i = offset + count - 1; i >= offset; i--) {
            sum += (bytes[i] & 0xff) << 8 * (i - offset);
        }
        return sum;
    }

    // 把低位在前高位在后的字节数组变成整型
    public static int byte2IntBack(ArrayList<Byte> bytes, int offset, int count) {
        int sum = 0;
        for (int i = offset + count - 1; i >= offset; i--) {
            sum += (bytes.get(i) & 0xff) << 8 * (i - offset);
        }
        return sum;
    }

    /**
     * second to HH:MM:ss
     */
    public static String convertSecondsToTime(long seconds) {
        String timeStr;
        int hour;
        int minute;
        int second;
        if (seconds <= 0)
            return "00:00";
        else {
            minute = (int) seconds / 60;
            if (minute < 60) {
                second = (int) seconds % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = (int) (seconds - hour * 3600 - minute * 60);
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    private static String unitFormat(int i) {
        String retStr;
        if (i >= 0 && i < 10)
            retStr = "0" + i;
        else
            retStr = "" + i;
        return retStr;
    }

    //保留两位小数并转换成String类型
    public static String formattedDecimalToPercentage(double decimal) {
        //获取格式化对象
        NumberFormat nt = NumberFormat.getPercentInstance();
        //设置百分数精确度2即保留两位小数
        nt.setMinimumFractionDigits(2);
        nt.setMaximumFractionDigits(2);
        return nt.format(decimal);
    }

    //判断字符串是否是数字
    public static boolean isNumber(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    public static String getNumberStr(int number) {
        if (number <= 0) return "零";
        StringBuilder sb = new StringBuilder();
        int single;
        while (number > 0) {
            single = number % 10;
            number /= 10;
            switch (single) {
                case 0:
                    sb.append("零");
                    break;
                case 1:
                    sb.append("一");
                    break;
                case 2:
                    sb.append("二");
                    break;
                case 3:
                    sb.append("三");
                    break;
                case 4:
                    sb.append("四");
                    break;
                case 5:
                    sb.append("五");
                    break;
                case 6:
                    sb.append("六");
                    break;
                case 7:
                    sb.append("七");
                    break;
                case 8:
                    sb.append("八");
                    break;
                case 9:
                    sb.append("九");
                    break;
            }
        }
        return sb.reverse().toString();
    }

    public static String getNumberStrWithUnit(int number) {
        if (number <= 0) return "零";
        StringBuilder sb = new StringBuilder();
        int single; //当前位值
        int numberCount = 0; //当前位数
        while (number > 0) {
            single = number % 10;
            number /= 10;
            numberCount++;
            if (single > 0) {
                switch (numberCount) {
                    case 2:
                    case 6:
                    case 10:
                    case 14:
                        sb.append("十");
                        break;
                    case 3:
                    case 7:
                    case 11:
                    case 15:
                        sb.append("百");
                        break;
                    case 4:
                    case 8:
                    case 12:
                    case 16:
                        sb.append("千");
                        break;
                    case 5:
                    case 13:
                        sb.append("万");
                        break;
                    case 9:
                        sb.append("亿");
                        break;
                }
                switch (single) {
                    case 1:
                        if (numberCount != 2 || number > 0)
                            sb.append("一");
                        break;
                    case 2:
                        sb.append("二");
                        break;
                    case 3:
                        sb.append("三");
                        break;
                    case 4:
                        sb.append("四");
                        break;
                    case 5:
                        sb.append("五");
                        break;
                    case 6:
                        sb.append("六");
                        break;
                    case 7:
                        sb.append("七");
                        break;
                    case 8:
                        sb.append("八");
                        break;
                    case 9:
                        sb.append("九");
                        break;
                }
            }
        }
        return sb.reverse().toString();
    }
}
